//
//  AppointmentVC.m
//  easyTaxi
//
//  Created by ellenpage on 12-12-7.
//  Copyright (c) 2012年 ellenpage. All rights reserved.
//

#import "Constant.h"
#import "AppointmentVC.h"
#import "CustomActionSheet.h"
#import "ViewAnimation.h"
#import "PassValueClass.h"
#import "RequestCMD.h"
#import "SocketObject.h"
#import "MBProgressHUD.h"
#import "AppointmentCell.h"
#import "SysViewObject.h"
#import "ZhiFuView.h"

#define kRect1 CGRectMake(0, 0, 320, 500)

#define kSheetType1Key @"dateType"
#define kSheetType2Key @"timeType"

NSInteger idInt;//要删除的序列号

bool kIsChooseHandle;//手动,自动
BOOL userEnable;


@interface AppointmentVC ()<UITextFieldDelegate,UIActionSheetDelegate,UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate,MFMessageComposeViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *callBtn;

@property (weak, nonatomic) IBOutlet UIButton *btn1;
@property (weak, nonatomic) IBOutlet UIButton *btn2;

@property (strong, nonatomic) IBOutlet UIView       *daijiaView;
@property (retain, nonatomic) IBOutlet UIDatePicker *datePicker;
@property (retain, nonatomic) IBOutlet UIDatePicker *timePicker;

@property (weak, nonatomic) IBOutlet UITextField *dateTxtView;
@property (weak, nonatomic) IBOutlet UITextField *timeTxtView;
@property (weak, nonatomic) IBOutlet UITextField *dateTxtView_Daijia;
@property (weak, nonatomic) IBOutlet UITextField *timeTxtView_Daijia;

@property (weak, nonatomic) IBOutlet UILabel *costLbl;

@property (retain, nonatomic) UIActionSheet *startsheet;
@property (retain, nonatomic) UIActionSheet *timesheet;

@property (retain, nonatomic) UIView *blackView;

@property (weak, nonatomic) IBOutlet UITextField *departureTxtView;
@property (weak, nonatomic) IBOutlet UITextField *destinationTxtView;
@property (weak, nonatomic) IBOutlet UITextField *phoneTxtView;

@property (weak, nonatomic) IBOutlet UITextField *departureTxtView_Daijia;
@property (weak, nonatomic) IBOutlet UITextField *destinationTxtView_Daijia;
@property (weak, nonatomic) IBOutlet UITextField *phoneTxtView_Daijia;

@property (weak, nonatomic) IBOutlet UIButton *hadleBtn;
@property (weak, nonatomic) IBOutlet UIButton *autoBtn;

@property (strong, nonatomic) IBOutlet UIView *showTBView;

@property (strong, nonatomic) IBOutlet UIView *detailView;

@property (weak, nonatomic) IBOutlet UILabel *pubTimeLbl;
@property (weak, nonatomic) IBOutlet UILabel *requestIdlbl;

@property (weak, nonatomic) IBOutlet UILabel *dateLbl;
@property (weak, nonatomic) IBOutlet UILabel *timeLbl;
@property (weak, nonatomic) IBOutlet UILabel *departureLbl;
@property (weak, nonatomic) IBOutlet UILabel *destinationLbl;
@property (weak, nonatomic) IBOutlet UILabel *phoneLbl;
@property (weak, nonatomic) IBOutlet UILabel *handleOrAutoLbl;
@property (weak, nonatomic) IBOutlet UIButton *deleteBtn;

@property (retain, nonatomic) MBProgressHUD *HD;

@property (weak, nonatomic) IBOutlet UIView *detailView_Small;

@property (weak, nonatomic) IBOutlet UILabel *driverLbl;
@property (weak, nonatomic) IBOutlet UILabel *driverPhoneLbl;

@property (weak, nonatomic) IBOutlet UIButton *addBtn;

@property (weak, nonatomic) IBOutlet UILabel *daCheDouLbl;
@property (weak, nonatomic) IBOutlet UILabel *costDaCheDouLbl;

@property (weak, nonatomic) IBOutlet UIButton *addCoinsBtn;
@property (weak, nonatomic) IBOutlet UIButton *minusCoinsBtn;

@property (weak, nonatomic) IBOutlet UILabel *checkNoLbl;

- (IBAction)chooseHandle;
- (IBAction)chooseAuto;
- (IBAction)goYuYueView;
- (IBAction)goDaiJiaView;
- (void)done:(UIBarButtonItem *)sender;
- (IBAction)resignAllKeyBoard;
- (IBAction)requestDaChe;
- (IBAction)hideTBView;
- (IBAction)requestDaiJia;
- (IBAction)removerDetailView;
- (IBAction)deleteThisAppointment:(UIButton *)sender;
- (IBAction)showMyTBView;
- (IBAction)showSysView;
- (IBAction)callTheDriver;

- (IBAction)openBuyCoinsView;
- (IBAction)addCoins;
- (IBAction)minusCoins;



@end

@implementation AppointmentVC

#pragma mark - System Function - 系统函数

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self){}
    return self;
}

- (void)viewDidLoad
{
    [self loadData];
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [self resignAllKeyBoard];
    [self.view addSubview:[SysViewObject sharedIntance].sysView];
    [SysViewObject sharedIntance].sysView.hidden = YES;
    
    [YouMengEvent YouMengEvent:kDaiJia_YouMeng];
    
    _daCheDouLbl.text = [PassValueClass sharedIntance].daCheDouStr;
    _costDaCheDouLbl.text = @"0";
    [PassValueClass sharedIntance].costDaCheDou = @"0";
    [PassValueClass sharedIntance].typeStr      = @"0";
}

- (void)viewWillDisappear:(BOOL)animated
{
    [self resignAllKeyBoard];
}

- (void)viewDidUnload
{
    [self setDateTxtView:nil];
    [self setTimeTxtView:nil];
    [self setBtn1:nil];
    [self setBtn2:nil];
    [self setDaijiaView:nil];
    [self setHadleBtn:nil];
    [self setAutoBtn:nil];
    [self setDateTxtView_Daijia:nil];
    [self setTimeTxtView_Daijia:nil];
    [self setDepartureTxtView:nil];
    [self setDestinationTxtView:nil];
    [self setPhoneTxtView:nil];
    [self setDepartureTxtView_Daijia:nil];
    [self setDestinationTxtView_Daijia:nil];
    [self setPhoneTxtView_Daijia:nil];
    [self setMTableView:nil];
    [self setDateLbl:nil];
    [self setTimeLbl:nil];
    [self setDepartureLbl:nil];
    [self setDestinationLbl:nil];
    [self setPhoneLbl:nil];
    [self setHandleOrAutoLbl:nil];
    [self setDetailView:nil];
    [self setDeleteBtn:nil];
    [self setShowTBView:nil];
    [self setDetailView_Small:nil];
    [self setPubTimeLbl:nil];
    [self setRequestIdlbl:nil];
    [self setPhoneBtn:nil];
    [self setCallBtn:nil];
    [self setDriverLbl:nil];
    [self setDriverPhoneLbl:nil];
    [self setAddBtn:nil];
    [self setCheckNoLbl:nil];
    [self setCostLbl:nil];
    [super viewDidUnload];
}

#pragma mark - Initial Data - 初始化数据

- (void)loadData
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getYuYueList) name:kGetYuYueListNoti object:nil];//得到预约列表
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getDaiJiaList) name:kGetDaiJiaListNoti object:nil];//得到代驾列表
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(getDaCheDou)
                                                 name:kGetDaCheDouNoti
                                               object:nil];//获得打车豆通知

    _daijiaView.frame = kRect1;
//    CGRectMake(0, 0, 320, 416);
    [self.view addSubview:_daijiaView];
    _daijiaView.hidden = YES;

    _blackView = [[UIView alloc] initWithFrame:kRect1];
    _blackView.backgroundColor = [UIColor blackColor];
    [self.view insertSubview:_blackView belowSubview:_daijiaView];
    _blackView.hidden = YES;
    
    _datePicker = [[UIDatePicker alloc] init];
    _datePicker.datePickerMode = UIDatePickerModeDate;
    
    _timePicker = [[UIDatePicker alloc] init];
    _timePicker.datePickerMode = UIDatePickerModeTime;

    _dateTxtView.delegate = self;
    _timeTxtView.delegate = self;
    
    NSString *phoneStr = [PassValueClass sharedIntance].teleNumStr;
    if (![phoneStr isEqualToString:nil])
    {
        _phoneTxtView.text        = phoneStr;
        _phoneTxtView_Daijia.text = phoneStr;
    }
    
    [self goYuYueView];
    [self chooseHandle];
    
    _mTableView.delegate   = self;
    _mTableView.dataSource = self;
    
    _showTBView.frame = CGRectMake(0, 44, 320, 372);
    [self.view addSubview:_showTBView];
    _showTBView.hidden = YES;
    
    CGRect addRect = CGRectMake(104, iP5 ? 392 : 326, 112, 36);
    _addBtn.frame = addRect;
}


- (void)dealWithDetailView//detailView加入边框
{
    _detailView.backgroundColor = [[UIColor darkTextColor] colorWithAlphaComponent:0.65f];
    [_detailView_Small.layer setCornerRadius:10];
    [_detailView_Small.layer setBorderColor:[[UIColor whiteColor] CGColor]];//描边颜色
    [_detailView_Small.layer setBorderWidth:2.0];//描边粗细
}

- (IBAction)goYuYueView
{
    [self selectPage:1];
}

- (IBAction)goDaiJiaView
{
    [self selectPage:2];
}

- (void)selectPage:(NSInteger)i
{
    _showTBView.hidden = YES;
   
    [self resignAllKeyBoard];
    
    switch (i)
    {
        case 1:
        {
            if (_daijiaView.hidden == NO)
            {
                _blackView.hidden = NO;
                [ViewAnimation viewAnimation:3 view:_daijiaView];
                _daijiaView.hidden = YES;
                [self performSelector:@selector(hiddenBlackView) withObject:nil afterDelay:0.5f];
            }
            [_btn1 setImage:[UIImage imageNamed:@"tab_left1.png"]  forState:UIControlStateNormal];
            [_btn2 setImage:[UIImage imageNamed:@"tab_right0.png"] forState:UIControlStateNormal];
                        
            [self performSelector:@selector(requestYuYueList) withObject:nil afterDelay:0.5f];
        }
            break;
            
        case 2:
        {
            if (_daijiaView.hidden == YES)
            {
                _blackView.hidden = NO;
                [ViewAnimation viewAnimation:2 view:_daijiaView];
                _daijiaView.hidden = NO;
                [self performSelector:@selector(hiddenBlackView) withObject:nil afterDelay:1.0f];
            }
            [_btn2 setImage:[UIImage imageNamed:@"tab_right1.png"] forState:UIControlStateNormal];
            [_btn1 setImage:[UIImage imageNamed:@"tab_left0.png"]  forState:UIControlStateNormal];
            
            [self performSelector:@selector(requestDaiJiaList) withObject:nil afterDelay:0.9f];

        }
            break;
  
        default:
            break;
    }
}

- (void)loadTBView
{
    [_mTableView reloadData];
}

- (IBAction)chooseHandle
{
    kIsChooseHandle = YES;
    [_hadleBtn setImage:[UIImage imageNamed:@"checkbox_on.png"] forState:UIControlStateNormal];
    [_autoBtn  setImage:[UIImage imageNamed:@"checkbox_off.png"] forState:UIControlStateNormal];
}

- (IBAction)chooseAuto
{
    kIsChooseHandle = NO;
    [_hadleBtn setImage:[UIImage imageNamed:@"checkbox_off.png"] forState:UIControlStateNormal];
    [_autoBtn  setImage:[UIImage imageNamed:@"checkbox_on.png"] forState:UIControlStateNormal];
}

- (void)hiddenBlackView
{
    _blackView.hidden = YES;
}

#pragma mark - 

-(void)StartdateSheet:(NSString *)sheetType
{
    if ([sheetType isEqualToString:kSheetType1Key])
    {
        _startsheet = [[UIActionSheet alloc] init];
        _startsheet.tag = kActionSheet1;
//        NSLog(@"%d",_startsheet.tag);
        _startsheet = [self customMyActionSheet:_startsheet title:@"选择日期" height:256.0f];
        [_startsheet showInView:self.view];
        _datePicker.frame = CGRectMake(0, 40, 320, 216);
        [_startsheet addSubview:_datePicker];
    }
    else if ([sheetType isEqualToString:kSheetType2Key])
    {
        _timesheet = [[UIActionSheet alloc] init];
        _timesheet.tag = kActionSheet2;
//        NSLog(@"%d",_timesheet.tag);
        _timesheet = [self customMyActionSheet:_timesheet title:@"选择时间" height:256.0f];
        [_timesheet showInView:self.view];
        _timePicker.frame = CGRectMake(0, 40, 320, 216);
        [_timesheet addSubview:_timePicker];
    }
}

- (UIActionSheet *)customMyActionSheet:(UIActionSheet *)actionsheet title:(NSString *)title height:(float)height
{
    int theight = height - 40;
    int btnnum = theight/50;
    
    for(int i=0; i<btnnum; i++)
    {
        [actionsheet addButtonWithTitle:@""];
    }
    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    toolBar.barStyle = UIBarStyleBlackOpaque;
    UIBarButtonItem *titleButton = [[UIBarButtonItem alloc] initWithTitle:title
                                                                    style:UIBarButtonItemStylePlain
                                                                   target:nil
                                                                   action:nil];
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] init];
    rightButton.tag    = actionsheet.tag;
    rightButton.title  = @"完成";
    rightButton.style  = UIBarButtonItemStyleDone;
    rightButton.target = self;
    rightButton.action = @selector(done:);
    NSLog(@"%d",rightButton.tag);

    
    UIBarButtonItem *leftButton  = [[UIBarButtonItem alloc] initWithTitle:@"取消"
                                                                    style:UIBarButtonItemStyleBordered
                                                                   target:self
                                                                   action:@selector(docancel)];
    
    UIBarButtonItem *fixedButton  = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                                  target:nil
                                                                                  action:nil];
    
    NSArray *array = [[NSArray alloc] initWithObjects:leftButton,fixedButton,titleButton,fixedButton,rightButton,nil];
    [toolBar setItems: array];
    [actionsheet addSubview:toolBar];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 480, height-44)];
    view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [actionsheet addSubview:view];

    return actionsheet;
}

- (void)docancel
{
    [_startsheet dismissWithClickedButtonIndex:0 animated:YES];
    [_timesheet  dismissWithClickedButtonIndex:0 animated:YES];
}

- (void)done:(UIBarButtonItem *)sender
{
    int i = sender.tag;        
    switch (i)
    {
        case kActionSheet1:
        {
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            formatter.dateFormat = @"YYYY-MM-dd";
            NSDate *startDate    = _datePicker.date;
            NSString *dateStr    = [formatter stringFromDate:startDate];
            if (_daijiaView.hidden == YES)
            {
                _dateTxtView.text    = dateStr;
            }
            else
            {
                _dateTxtView_Daijia.text = dateStr;
            }
            [_startsheet dismissWithClickedButtonIndex:0 animated:YES];
        }
            break;
        case kActionSheet2:
        {
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            formatter.dateFormat = @"HH:mm";
            NSDate *startDate    = _timePicker.date;
            NSString *dateStr    = [formatter stringFromDate:startDate];
            
            if (_daijiaView.hidden == YES)
            {
                _timeTxtView.text    = dateStr;
            }
            else
            {
                _timeTxtView_Daijia.text = dateStr;
            }
            [_timesheet dismissWithClickedButtonIndex:0 animated:YES];
        }
            break;
            
        default:
            break;
    }
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField == _dateTxtView || textField == _dateTxtView_Daijia)
    {
        [self resignAllKeyBoard];
        [self StartdateSheet:kSheetType1Key];
    }
    if (textField == _timeTxtView || textField == _timeTxtView_Daijia)
    {
        [self resignAllKeyBoard];
        [self StartdateSheet:kSheetType2Key];
    }
}

- (void)resignAllKeyBoard
{
    [_dateTxtView resignFirstResponder];
    [_timeTxtView resignFirstResponder];
    
    [_departureTxtView   resignFirstResponder];
    [_destinationTxtView resignFirstResponder];
    [_phoneTxtView       resignFirstResponder];
    
    [_dateTxtView_Daijia resignFirstResponder];
    [_timeTxtView_Daijia resignFirstResponder];

    [_departureTxtView_Daijia   resignFirstResponder];
    [_destinationTxtView_Daijia resignFirstResponder];
    [_phoneTxtView_Daijia       resignFirstResponder];
}

- (IBAction)requestDaChe
{
    [self userLimits];
    if (userEnable == YES)
    {
    if (_dateTxtView.text != nil && _timeTxtView.text != nil && _destinationTxtView.text != nil && _departureTxtView.text != nil && _phoneTxtView.text != nil)
    {
     
        [PassValueClass sharedIntance].appointTimeStr = _timeTxtView.text;
        [PassValueClass sharedIntance].appointDateStr = _dateTxtView.text;
        [PassValueClass sharedIntance].appointDepartureStr   = _departureTxtView.text;
        [PassValueClass sharedIntance].appointDestinationStr = _destinationTxtView.text;
        [PassValueClass sharedIntance].teleNumStr = _phoneTxtView.text;
        
        //--------------------1035---预约打车
        NSString *string    = [RequestCMD requestCMD:kTaxiAppointment];
        NSMutableData *data = [RequestCMD getWantedData:string];
        [[SocketObject sharedInstance].gcdSocket writeData:data withTimeout:-1 tag:kTaxiAppointment];//1035---预约打车
    }
    else
    {
        UIAlertView *notFinishAlert = [[UIAlertView alloc] initWithTitle:@"请输入完整的信息"
                                                                 message:nil
                                                                delegate:self
                                                       cancelButtonTitle:nil
                                                       otherButtonTitles:@"知道了", nil];
        [notFinishAlert show];
    }
  }
}


- (IBAction)requestDaiJia
{
    [self userLimits];
    if (userEnable == YES)
    {    
    if (_dateTxtView_Daijia.text != nil && _timeTxtView_Daijia.text != nil && _destinationTxtView_Daijia.text != nil && _departureTxtView_Daijia.text != nil && _phoneTxtView_Daijia.text != nil)
    {
        [PassValueClass sharedIntance].appointTimeStr_Daijia = _timeTxtView_Daijia.text;
        [PassValueClass sharedIntance].appointDateStr_Daijia = _dateTxtView_Daijia.text;
        [PassValueClass sharedIntance].appointDepartureStr_Daijia   = _departureTxtView_Daijia.text;
        [PassValueClass sharedIntance].appointDestinationStr_Daijia = _destinationTxtView_Daijia.text;
        [PassValueClass sharedIntance].teleNumStr = _phoneTxtView_Daijia.text;

        if (kIsChooseHandle == YES)
        {
            [PassValueClass sharedIntance].cllxStr = [NSString stringWithFormat:@"%d",0];
        }
        else
        {
            [PassValueClass sharedIntance].cllxStr = [NSString stringWithFormat:@"%d",1];
        }
        
        //--------------------1027---预约代驾
        NSString *string    = [RequestCMD requestCMD:kDesignatedDriveInfo];
        NSMutableData *data = [RequestCMD getWantedData:string];
        [[SocketObject sharedInstance].gcdSocket writeData:data withTimeout:-1 tag:kDesignatedDriveInfo];//1035---预约打车

    }
    else
    {
        UIAlertView *notFinishAlert = [[UIAlertView alloc] initWithTitle:@"请输入完整的信息"
                                                                 message:nil
                                                                delegate:self
                                                       cancelButtonTitle:nil
                                                       otherButtonTitles:@"知道了", nil];
        [notFinishAlert show];
    }
  }
}

- (IBAction)hideTBView
{
    _showTBView.hidden = YES;
}

- (IBAction)showMyTBView
{
    
    NSArray *dateArray        = [PassValueClass sharedIntance].yuYueHistoryArray;
    NSArray *dateArray_Daijia = [PassValueClass sharedIntance].daJiaHistoryArray;

    if (_daijiaView.hidden == YES)
    {
        _dataArray = [NSMutableArray arrayWithArray:dateArray];
    }
    else
    {
        _dataArray_Daijia = [NSMutableArray arrayWithArray:dateArray_Daijia];
    }

    _showTBView.hidden = NO;
    [_mTableView reloadData];
}

- (IBAction)showSysView
{
    if ([SysViewObject sharedIntance].sysView.hidden == YES)
    {
        [SysViewObject sharedIntance].sysView.hidden = NO;
    }
}


#pragma mark - TableViewDataSource Delegate - TableView Datasource代理

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 95;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_daijiaView.hidden == YES)
    {
        if (_dataArray.count > 0)
        {
            _showTBView.hidden = NO;
        }

        return [_dataArray count];
    }
    else
    {
        if (_dataArray_Daijia.count > 0)
        {
            _showTBView.hidden = NO;
        }

        return [_dataArray_Daijia count];
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AppointmentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AppointmentCell"];
    if (!cell)
    {
        cell = [AppointmentCell getInstance];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    NSString *desStr;    //目的地
    NSString *depureStr; //起始地
    NSString *dateStr;   //预约时间
    NSString *statusStr; //状态值
    NSString *clllStr;   //车辆类型
    NSString *checkNoStr;//校验码
    
    if (_daijiaView.hidden == YES)
    {
        desStr     = [[_dataArray objectAtIndex:indexPath.row] objectForKey:kDestination];
        dateStr    = [[_dataArray objectAtIndex:indexPath.row] objectForKey:kAppointmentTime];
        depureStr  = [[_dataArray objectAtIndex:indexPath.row] objectForKey:kDeparture];
        statusStr  = [[_dataArray objectAtIndex:indexPath.row] objectForKey:kStatus];
        checkNoStr = [[_dataArray objectAtIndex:indexPath.row] objectForKey:kCheckNo];
    }
    else
    {
        desStr    = [[_dataArray_Daijia objectAtIndex:indexPath.row] objectForKey:kDestination];
        dateStr   = [[_dataArray_Daijia objectAtIndex:indexPath.row] objectForKey:kAppointmentTime];
        depureStr = [[_dataArray_Daijia objectAtIndex:indexPath.row] objectForKey:kDeparture];
        clllStr   = [[_dataArray_Daijia objectAtIndex:indexPath.row] objectForKey:kCLLX];
        statusStr = [[_dataArray_Daijia objectAtIndex:indexPath.row] objectForKey:kStatus];
    }
    
    cell.showDetailBtn.tag = indexPath.row;
    
    if ([statusStr isEqualToString:@"0"])
    {
        cell.statusImgView.image = [UIImage imageNamed:@"nonreceivedpic.png"];
    }
    else if ([statusStr isEqualToString:@"1"])
    {
        cell.statusImgView.image = [UIImage imageNamed:@"receivedpic.png"];
    }
    
    [cell.showDetailBtn addTarget:self action:@selector(showDetailView:) forControlEvents:UIControlEventTouchUpInside];
    
    cell.depureLbl.text = depureStr;
    cell.desLbl.text    = desStr;
    cell.publishTimeLbl.text = dateStr;
    cell.checkNoLbl.text     = checkNoStr;

    
	return cell;
}

- (void)showDetailView:(UIButton *)sender
{
    int i = sender.tag;
    _detailView.tag = i;
    [self initialDetailData];
    [self dealWithDetailView];
    [self.view addSubview:_detailView];
}

- (void)initialDetailData
{
    int i = _detailView.tag;
    NSDictionary *dict;
    NSString *idStr;
    
    if (_daijiaView.hidden == YES)
    {
        dict  = [_dataArray objectAtIndex:i];
        idStr = [dict objectForKey:kAppointmentID];
    }
    else
    {
        dict  = [_dataArray_Daijia objectAtIndex:i];
        idStr = [dict objectForKey:kThirdDrivingID];
    }
    
    NSString *timeStr     = [dict objectForKey:kAppointmentTime];
    NSString *depeture    = [dict objectForKey:kDeparture];
    NSString *destination = [dict objectForKey:kDestination];
    NSString *phoneStr    = [dict objectForKey:kTelephone];
    NSString *pubTimeStr  = [dict objectForKey:kPublishTime];
    NSString *statusStr   = [dict objectForKey:kStatus];
    NSString *driverPhone = [dict objectForKey:kDriverPhoen];
    NSString *checkNoStr  = [dict objectForKey:kCheckNo];
    NSString *costStr     = [dict objectForKey:kCoin];

    _requestIdlbl.text   = idStr;
    _timeLbl.text        = timeStr;
    _departureLbl.text   = depeture;
    _destinationLbl.text = destination;
    _phoneLbl.text       = phoneStr;
    _pubTimeLbl.text     = pubTimeStr;
    _checkNoLbl.text     = checkNoStr;
    _costLbl.text        = costStr;
    
    if (_daijiaView.hidden == NO)
    {
        NSString *handleOrAuto = [dict objectForKey:kCLLX];
        if ([handleOrAuto isEqualToString:@"0"])
        {
            _handleOrAutoLbl.text = @"手动";
        }
        else
        {
            _handleOrAutoLbl.text = @"自动";
        }
    }
    
    if ([statusStr isEqualToString:@"0"])//未接单--
    {
        _driverLbl.hidden = YES;
        _driverPhoneLbl.hidden = YES;
        _phoneBtn.hidden = YES;
        
    }
    else if ([statusStr isEqualToString:@"1"])//已接单--
    {
        _driverLbl.hidden = NO;
        _driverPhoneLbl.hidden = NO;
        _phoneBtn.hidden = NO;
        
        _driverPhoneLbl.text = driverPhone;
    }
    _deleteBtn.tag = i;
}

- (IBAction)callTheDriver
{
    NSString *phoneStr = _driverPhoneLbl.text;
    if (phoneStr != nil && [phoneStr length] == 11)
    {
        UIWebView*callWebview =[[UIWebView alloc] init];
        NSString *str = [NSString stringWithFormat:@"tel:%@",phoneStr];
        NSURL *telURL =[NSURL URLWithString:str];// 貌似tel:// 或者 tel: 都行
        [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
        [self.view addSubview:callWebview];
        
        if (_daijiaView.hidden == YES)
        {
            [YouMengEvent YouMengEvent:kCallDriverYuYue_YouMeng];
        }
        else
        {
            [YouMengEvent YouMengEvent:kCallDriverDaiJia_YouMeng];
        }
    }
}

- (IBAction)removerDetailView
{
    [_detailView removeFromSuperview];
}

- (IBAction)deleteThisAppointment:(UIButton *)sender
{
    UIAlertView *newAlertView = [[UIAlertView alloc] initWithTitle:@"确定要删除这个订单吗?" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    newAlertView.tag = sender.tag;
    [newAlertView show];
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    int i = alertView.tag;
    
    if (buttonIndex == 1)
    {
        
        NSMutableArray *mArray;
        
        if (_daijiaView.hidden == YES)
        {
             mArray = [PassValueClass sharedIntance].yuYueHistoryArray;
        }
        else
        {
            mArray = [PassValueClass sharedIntance].daJiaHistoryArray;
        }
        NSDictionary *tmpDict = [mArray objectAtIndex:i];
        
        if (_daijiaView.hidden == YES)
        {
            NSString *idStr = [tmpDict objectForKey:kAppointmentID];
            [PassValueClass sharedIntance].tmpAppointmentID = idStr;
            

            //--------------------1041---取消预约打车
            NSString *string    = [RequestCMD requestCMD:kTaxiAppointmentCancel];
            NSMutableData *data = [RequestCMD getWantedData:string];
            [[SocketObject sharedInstance].gcdSocket writeData:data withTimeout:-1 tag:kTaxiAppointmentCancel];//1041---取消预约打车
            idInt = i;
            
            if (!_HD)
            {
                _HD = [[MBProgressHUD alloc] initWithView:self.view];
                [self.view addSubview:_HD];
                _HD.labelText = @"请稍后";
            }
            [_HD showAnimated:YES whileExecutingBlock:^{
                //对话框显示时需要执行的操作
                sleep(2);
            } completionBlock:^{
                //操作执行完后取消对话框
                [_HD removeFromSuperview];
                _HD = nil;
                [_detailView removeFromSuperview];}];
        }
        else
        {
            NSString *idStr = [tmpDict objectForKey:kThirdDrivingID];
            [PassValueClass sharedIntance].tmpThirdDriverID = idStr;
            
            //--------------------1033---取消预约代驾
            NSString *string    = [RequestCMD requestCMD:kDesignatedDriveCancel];
            NSMutableData *data = [RequestCMD getWantedData:string];
            [[SocketObject sharedInstance].gcdSocket writeData:data withTimeout:-1 tag:kDesignatedDriveCancel];//1033---取消预约代驾
            
            idInt = i;

            if (!_HD)
            {
                _HD = [[MBProgressHUD alloc] initWithView:self.view];
                [self.view addSubview:_HD];
                _HD.labelText = @"请稍后";
            }
            [_HD showAnimated:YES whileExecutingBlock:^{
                //对话框显示时需要执行的操作
                sleep(2);
            } completionBlock:^{
                //操作执行完后取消对话框
                [_HD removeFromSuperview];
                _HD = nil;
                [_detailView removeFromSuperview];}];
        }
    }
}

#pragma mark - NSNotificationer - 接受通知/发起通知

- (void)getYuYueList
{
    _dataArray = [NSArray arrayWithArray:[PassValueClass sharedIntance].yuYueHistoryArray];
    [self loadTBView];
}


- (void)getDaiJiaList
{
    _dataArray_Daijia = [NSArray arrayWithArray:[PassValueClass sharedIntance].daJiaHistoryArray];
    [self loadTBView];
}

- (void)requestYuYueList
{
    NSString *string    = [RequestCMD requestCMD:kTaxiAppointmentCheck];//请求预约列表
    NSMutableData *data = [RequestCMD getWantedData:string];
    [[SocketObject sharedInstance].gcdSocket writeData:data withTimeout:-1 tag:kTaxiAppointmentCheck];
}

- (void)requestDaiJiaList
{
    NSString *string    = [RequestCMD requestCMD:kDesignatedDriveCheck];//请求代驾列表
    NSMutableData *data = [RequestCMD getWantedData:string];
    [[SocketObject sharedInstance].gcdSocket writeData:data withTimeout:-1 tag:kDesignatedDriveCheck];
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([string isEqualToString:@"\n"])
    {
        [_dateTxtView resignFirstResponder];
        [_timeTxtView resignFirstResponder];
        
        [_dateTxtView_Daijia resignFirstResponder];
        [_timeTxtView_Daijia resignFirstResponder];
        
        [_departureTxtView   resignFirstResponder];
        [_destinationTxtView resignFirstResponder];
        [_phoneTxtView       resignFirstResponder];
        
        [_departureTxtView_Daijia   resignFirstResponder];
        [_destinationTxtView_Daijia resignFirstResponder];
        [_phoneTxtView_Daijia       resignFirstResponder];

        return NO;
    }
    return YES;
}

- (void)getDaCheDou
{
    _daCheDouLbl.text = [PassValueClass sharedIntance].daCheDouStr;
    _costDaCheDouLbl.text = @"0";
    [PassValueClass sharedIntance].costDaCheDou = @"0";
    [PassValueClass sharedIntance].typeStr      = @"0";
}

#pragma mark - 用户权限管理

- (void)userLimits
{
    if (isTiYanMoShiOk)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"当前处在体验模式" message:@"体验模式下仅能查看信息" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
        [alert show];
        userEnable = NO;
        return;
    }

    
    if (!isSokectOk)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"服务器未连接,请稍后重试"
                                                        message:nil
                                                       delegate:self
                                              cancelButtonTitle:@"好的"
                                              otherButtonTitles:nil, nil];
        [alert show];
        userEnable = NO;
        return;
    }
    
    if (!isAvaliableCityOk)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"你所在的区域不支持我们的服务,敬请期待~"
                                                        message:nil
                                                       delegate:self
                                              cancelButtonTitle:@"好的"
                                              otherButtonTitles:nil, nil];
        [alert show];
        userEnable = NO;
        return;
    }

    if (!isLoginOk)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络问题导致登录异常,请稍后重试"
                                                        message:nil
                                                       delegate:self
                                              cancelButtonTitle:@"好的"
                                              otherButtonTitles:nil, nil];
        [alert show];
        userEnable = NO;
        return;
    }
    
    //3.判断是否已经绑定手机号
    NSString *isBindedStr = [PassValueClass sharedIntance].isBindedStr;
    if (isBindedStr == nil)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"正在获取个人信息请稍后..."
                                                        message:nil
                                                       delegate:self
                                              cancelButtonTitle:@"好的"
                                              otherButtonTitles:nil, nil];
        [alert show];
        userEnable = NO;
        return;
        
    }
    
    if ([isBindedStr isEqualToString:@"0"])
    {
        if (!kIsIphone)
        {
            UIAlertView *signUpAlertView = [[UIAlertView alloc] initWithTitle:@"检测到您的设备不具备短信和运营商电话功能,请使用iPhone体验本程序~"
                                                                      message:nil
                                                                     delegate:self
                                                            cancelButtonTitle:@"知道了"
                                                            otherButtonTitles:nil, nil];
            
            [signUpAlertView show];
            userEnable = NO;
            return;
        }
        else
        {
            UIAlertView *signUpAlertView = [[UIAlertView alloc] initWithTitle:@"手机注册"
                                                                      message:@"为了您的交易安全,请发送一条短信,完成注册"
                                                                     delegate:self
                                                            cancelButtonTitle:@"稍后再试"
                                                            otherButtonTitles:@"发送短信", nil];
            
            signUpAlertView.tag = kUserLogInAlert;
            [signUpAlertView show];
            userEnable = NO;
            return;
        }
    }
    userEnable = YES;
}

#pragma mark - DaCheDou - 打车豆相关

- (IBAction)openBuyCoinsView
{
    ZhiFuView *zhifuView = [[ZhiFuView alloc] init];
    [self.view addSubview:zhifuView];
}

- (IBAction)addCoins
{
    if ([_daCheDouLbl.text intValue] < 50)
    {
        return;
    }
    
    if ([_daCheDouLbl.text intValue] >= 50)
    {
        _daCheDouLbl.text     = [NSString stringWithFormat:@"%d",[_daCheDouLbl.text intValue]     - 50];
        _costDaCheDouLbl.text = [NSString stringWithFormat:@"%d",[_costDaCheDouLbl.text intValue] + 50];
        
        if ([_costDaCheDouLbl.text intValue] >= 50)
        {
            [PassValueClass sharedIntance].costDaCheDou = [NSString stringWithFormat:@"%@",_costDaCheDouLbl.text];
            [PassValueClass sharedIntance].typeStr = @"1";
        }
    }
}

- (IBAction)minusCoins
{
    if ([_costDaCheDouLbl.text intValue] < 50)
    {
        return;
    }
    
    if ([_costDaCheDouLbl.text intValue] >= 50)
    {
        _daCheDouLbl.text     = [NSString stringWithFormat:@"%d",[_daCheDouLbl.text intValue]     + 50];
        _costDaCheDouLbl.text = [NSString stringWithFormat:@"%d",[_costDaCheDouLbl.text intValue] - 50];
    
        if ([_costDaCheDouLbl.text intValue] == 0)
        {
            [PassValueClass sharedIntance].costDaCheDou = [NSString stringWithFormat:@"%@",_costDaCheDouLbl.text];
            [PassValueClass sharedIntance].typeStr = @"0";
        }
    }
}


@end
